<template>
  <ul class="app-header-nav">
    <li class="home">
      <RouterLink to="/">首页</RouterLink>
    </li>
    <li v-for="(item, key) in cateList" :key="item.id">
      <a @click="setColor(key, item.id)" :class="{ cateActive: key == $store.state.category.nowCateIndex }">{{ item.name }}</a>
      <div class="layer">
        <ul>
          <li v-for="i in item.children" :key="i.id">
            <router-link :to="'/subCate?id='+i.id">
              <img :src="i.picture" alt="">
              <p>{{ i.name }}</p>
            </router-link> 
          </li>
        </ul>
      </div>
    </li>
  </ul>
</template>

<script>
export default {
  computed: {
    cateList() {
      return this.$store.state.category.cate
    }
  },
  methods: {
    setColor(k, id) {
      //将k保存到vuex中
      this.$store.commit('category/setCateIndex', k)
      //跳转页面
      this.$router.push('/cate?id=' + id)
    }
  }
}
</script>  

<style scoped lang='scss'>
@import "../assets/css/color.scss";

@mixin set($color) {
  //   font-size: $size;
  color: $color;
}

@mixin set1($background-color) {
  //   font-size: $size;
  background-color: $background-color;
}

.app-header-nav {
  width: 820px;
  display: flex;
  justify-content: space-around;
  padding-left: 40px;
  position: relative;
  z-index: 998;

  >li {
    margin-right: 40px;
    width: 38px;
    text-align: center;

    >a {
      font-size: 16px;
      line-height: 32px;
      height: 32px;
      display: inline-block;
    }

    &:hover {
      >a {
        @include set($xtxColor);
        border-bottom: 1px solid #27ba9b;
      }

      >.layer {
        height: 132px;
        opacity: 1;
      }
    }
  }
}

.layer {
  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 56px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  box-shadow: 0 0 5px #ccc;
  transition: all 0.2s 0.1s;

  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;

    li {
      width: 110px;
      text-align: center;

      img {
        width: 60px;
        height: 60px;
      }

      p {
        padding-top: 10px;
      }

      &:hover {
        p {
          @include set($xtxColor);
        }
      }
    }
  }
}

.cateActive {
  @include set($xtxColor);
  border-bottom: 1px solid #27ba9b;
}
</style>